<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        let input=`15
        10 20 30 40 60 60 70 80 90 150`
        let pi=input.split('\n')
        let m=parseInt(pi.shift())
        pi=[0,...pi[0].trim().split(' ').map(v=>parseInt(v))]

        let list=new Array(m+1).fill(0)
        
        for(let i=1;i<pi.length;i++){
            let val=pi[i]
            for(let j=i;j<=m;j++){
                list[j]=Math.max(list[j],val+list[j-i])
            }
        }
       
        console.log(list[m]);
        
       
        
    </script>
</body>
</html>